package com.freesky.quartz.task;

import com.alibaba.fastjson2.JSON;
import com.freesky.biz.activity.domain.TRechargeDetail;
import com.freesky.biz.activity.mapper.RechargeDetailMapper;
import com.freesky.biz.external.service.MobileCmccService;
import com.freesky.biz.external.service.MobileJuCaiService;
import com.freesky.biz.external.service.MobileJulivesService;
import com.freesky.biz.external.service.MobileWpoolService;
import com.freesky.biz.rights.domain.RightsMobileRechargeEntity;
import com.freesky.biz.rights.mapper.RightsMobileRechargeMapper;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.config.Constant;
import com.freesky.common.constant.Constants;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.utils.StringUtils;
import com.freesky.framework.web.service.SendAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 通用话费充值服务
 */
@Component("mobileRechargeTask")
public class RightsMobileRechargeTask {

    private static final Logger logger = LoggerFactory.getLogger(PointExchangeTask.class);

    @Autowired
    private RightsMobileRechargeMapper mobileRechargeMapper;

    @Autowired
    private MobileCmccService mobileCmccService;

    @Autowired
    private MobileJuCaiService mobileJuCaiService;

    @Autowired
    private MobileWpoolService mobileWpoolService;

    @Autowired
    private SendAlarmService sendAlarmService;

    public void taskProcess() {
        List<RightsMobileRechargeEntity> mobilRechargeList = mobileRechargeMapper.selectUnRechargeList();
        for(RightsMobileRechargeEntity mobileRechargeEntity : mobilRechargeList){
            int nSendCount = mobileRechargeMapper.getRechargeMobileLogCount(mobileRechargeEntity.getId());
            if (nSendCount >= 3) {
                mobileRechargeMapper.updateProcessedById(mobileRechargeEntity.getId(), "5");
                sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL, "【异常】JOB-话费充值（通用）-超过三次",
                        "次数【" + nSendCount + "】：" + JSON.toJSONString(mobileRechargeEntity));
                continue;
            }

            //防误更改操作
            if(StringUtils.isNotEmpty(mobileRechargeEntity.getVoucherNo())){
                mobileRechargeMapper.updateProcessedById(mobileRechargeEntity.getId(), "6");
            } else {
                mobileRechargeMapper.updateProcessedById(mobileRechargeEntity.getId(), "1");
                processRecharge(mobileRechargeEntity);
            }
        }
    }


    //处理充值
    public void processRecharge(RightsMobileRechargeEntity mobileRecharge){
        if(StringUtils.isEmpty(mobileRecharge.getChannel())){
            sendMobileCmccRecharge(mobileRecharge);
        } else if("移动".equalsIgnoreCase(mobileRecharge.getChannel())){
            sendMobileRechargeJuCai(mobileRecharge);
        } else if("聚采".equalsIgnoreCase(mobileRecharge.getChannel())){
            sendMobileRechargeWpool(mobileRecharge);
        } else {
            sendMobileRechargeJuCai(mobileRecharge);
        }
    }

    //移动充值
    public void sendMobileCmccRecharge(RightsMobileRechargeEntity rechargeDetail){
        try{
            rechargeDetail.setLastReqId(SeqUtil.getSeqId());
            AjaxResult result = mobileCmccService.sendMobileRecharge(
                    rechargeDetail.getLastReqId(),
                    String.valueOf(rechargeDetail.getAmt()*10),
                    rechargeDetail.getReceiveAccount());

            rechargeDetail.setChannel("移动");
            if(result.isSuccess()) {
                rechargeDetail.setRespCode(Constants.SUCCESS);
                rechargeDetail.setRespErrmsg("充值成功");
                rechargeDetail.setProcessed("4");
                rechargeDetail.setRechargeId(rechargeDetail.getLastReqId());
                rechargeDetail.setVoucherNo(rechargeDetail.getLastReqId());
                rechargeDetail.setState("1");
            } else {
                rechargeDetail.setRespCode(Constants.FAIL);
                rechargeDetail.setRespErrmsg(result.getMsg());
                rechargeDetail.setProcessed("3");
            }
            mobileRechargeMapper.updateRechargeResultById(rechargeDetail);
            mobileRechargeMapper.insertIntegralMobileLog(rechargeDetail);
        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL,"【异常】JOB-话费充值-移动接口",ioe.getMessage());
        }
    }

    //卡池
    public void sendMobileRechargeWpool(RightsMobileRechargeEntity rechargeDetail){
        try{
            rechargeDetail.setLastReqId(SeqUtil.getSeqId());
            AjaxResult result = mobileWpoolService.sendMobileRecharge(
                    "RMR_" + rechargeDetail.getLastReqId(),
                    String.valueOf(rechargeDetail.getAmt()),
                    rechargeDetail.getReceiveAccount());

            rechargeDetail.setChannel("卡池");
            rechargeDetail.setRespCode(result.get("code").toString());
            rechargeDetail.setRespErrmsg(result.getMsg());
            if(result.isSuccess()) {
                String orderSn = result.get("data").toString();
                rechargeDetail.setRechargeId(orderSn);
                rechargeDetail.setProcessed("2");
            } else {
                rechargeDetail.setProcessed("3");
            }
            mobileRechargeMapper.updateRechargeResultById(rechargeDetail);
            mobileRechargeMapper.insertIntegralMobileLog(rechargeDetail);
        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL,"【异常】JOB-话费充值-卡池接口",ioe.getMessage());
        }
    }

    public void sendMobileRechargeJuCai(RightsMobileRechargeEntity rechargeDetail){
        try{
            rechargeDetail.setLastReqId(SeqUtil.getSeqId());
            AjaxResult result = mobileJuCaiService.sendMobileRecharge(
                    "RMR_" + rechargeDetail.getLastReqId(),
                    String.valueOf(rechargeDetail.getAmt()),
                    rechargeDetail.getReceiveAccount());
            rechargeDetail.setChannel("聚采");
            rechargeDetail.setRespCode(result.get("code").toString());
            rechargeDetail.setRespErrmsg(result.getMsg());

            if(result.isSuccess()){
                rechargeDetail.setRechargeId("");
                rechargeDetail.setRechargeId(rechargeDetail.getLastReqId());
                rechargeDetail.setProcessed("2");
            } else {
                rechargeDetail.setProcessed("3");
            }
            mobileRechargeMapper.updateRechargeResultById(rechargeDetail);
            mobileRechargeMapper.insertIntegralMobileLog(rechargeDetail);
        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL,"【异常】JOB-话费充值-聚采接口",ioe.getMessage());
        }
    }

}
